package com.yonyou.pmclouds.externalservice.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.entity.Entity;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.externalservice.entity.FailedExternalRecordVO;
import com.yonyou.pmclouds.externalservice.mapper.FailedExternalRecordMapper;
import com.yonyou.pmclouds.externalservice.rmiitf.FailedExternalRecordMaintain;
import com.yonyou.pmclouds.messagenotice.constructor.impl.MessageNoticeConstructor;
import com.yonyou.pmclouds.messagenotice.entity.MessageNoticeVO;
import com.yonyou.pmclouds.workflow.service.rmiitf.IWorkFlowService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = FailedExternalRecordMaintain.class)
public class FailedExternalRecordMaintainImpl implements FailedExternalRecordMaintain {

    private final FailedExternalRecordMapper failedRecordMapper;

    private final MessageNoticeConstructor noticeConstructor;

    private final IWorkFlowService workFlowService;

    @Autowired
    public FailedExternalRecordMaintainImpl(FailedExternalRecordMapper failedRecordMapper, MessageNoticeConstructor noticeConstructor, IWorkFlowService workFlowService) {
        this.failedRecordMapper = failedRecordMapper;
        this.noticeConstructor = noticeConstructor;
        this.workFlowService = workFlowService;
    }

    @Override
    public FailedExternalRecordVO insert(SuperVO businessVO, String failedReason) throws BusinessException {
        if(businessVO == null){
            return null;
        }

        // 保存之前先校验该单据是否生成过失败记录，如果存在，不再重复生成
        FailedExternalRecordVO recordVO = failedRecordMapper.queryRecordByPkBill(businessVO.pkFiledValue().toString());
        if(recordVO == null){
            recordVO = coverBusinessVoToRecordVO(businessVO, failedReason);
            SingleInsertBpTemplate<FailedExternalRecordVO> insertBpTemplate = new SingleInsertBpTemplate<>(failedRecordMapper);
            insertBpTemplate.addBeforeProcessor(new AuditInfoInsertProcessor<FailedExternalRecordVO>());
            insertBpTemplate.insert(recordVO);
        }

        return recordVO;
    }

    private FailedExternalRecordVO coverBusinessVoToRecordVO(SuperVO businessVO, String failedReason) throws BusinessException {
        FailedExternalRecordVO recordVO = new FailedExternalRecordVO();
        recordVO.setPkTenant(RuntimeEnvironment.getTenantId());
        recordVO.setMessageReceiver(RuntimeEnvironment.getLoginUserId());
        recordVO.setServiceSrc(FailedExternalRecordVO.SERVER_SRC_APPROVAL);
        recordVO.setRelateKey(businessVO.pkFiledValue().toString());
        String billType = StringUtils.isEmpty(getFieldValue(businessVO, CommonFieldConst.BILL_TYPE))
                ? getFieldValue(businessVO, CommonFieldConst.BILLTYPE)
                : getFieldValue(businessVO, CommonFieldConst.BILL_TYPE);
        recordVO.setRelateType(billType);
        recordVO.setFailureReason(failedReason);

        MessageNoticeVO[] messageNoticeVOS = noticeConstructor.generateNotice(businessVO);
        if(messageNoticeVOS != null && messageNoticeVOS.length > 0){
            recordVO.setMessageContent(messageNoticeVOS[0].getMessageContent());
            recordVO.setMessageTitle(messageNoticeVOS[0].getMessageTitle());
        }

        return recordVO;
    }

    private String getFieldValue(Entity vo, String fieldName) {
        Object fieldValue = BeanHelper.getProperty(vo, fieldName);
        if (fieldValue == null) {
            return null;
        }
        return (String) fieldValue;
    }

    @Override
    public FailedExternalRecordVO deleteRecordWithBill(String pkBill, String billType) throws BusinessException {
        FailedExternalRecordVO recordVO = failedRecordMapper.queryRecordByPkBill(pkBill);
        if(recordVO != null){
            SingleDeleteBpTemplate<FailedExternalRecordVO> deleteBpTemplate = new SingleDeleteBpTemplate<>(failedRecordMapper);
            recordVO = deleteBpTemplate.delete(recordVO);
            workFlowService.deleteBill(pkBill, billType);

        }
        return recordVO;
    }

    @Override
    public FailedExternalRecordVO delete(FailedExternalRecordVO recordVO) throws BusinessException {
        if(recordVO == null){
            return null;
        }
        SingleDeleteBpTemplate<FailedExternalRecordVO> deleteBpTemplate = new SingleDeleteBpTemplate<>(failedRecordMapper);
        return deleteBpTemplate.delete(recordVO);
    }
}
